package com.glp.work.flow.service.impl;

import com.glp.common.util.DateUtil;
import com.glp.work.flow.constant.APICode;
import com.glp.work.flow.exception.APIException;
import com.glp.work.flow.mapper.CodeMapper;
import com.glp.work.flow.pojo.Code;
import com.glp.work.flow.service.CodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 业务系统流水号公共服务。
 *
 * @author Pan Wang
 * @time 2019-10-28 15:25:01
 */

@Service
public class CodeServiceImpl implements CodeService {

    /**
     * 流水的平台属性
     */
    @Value("${code.platform}")
    private Long codePlatform;

    /**
     * 最小流水号
     */
    @Value("${code.startIncrement}")
    private Long codeStart;

    @Autowired
    private CodeMapper codeMapper;


    /**
     * 流水号生成
     *
     * @param factor 生成因素
     */
    @Override
    public String generate(String factor) {
        if (StringUtils.isBlank(factor)) {
            throw new APIException(APICode.NULL_PARAMETER, "物件序列生成的因素为空！");
        }
        Code code = new Code();
        code.setFactor(factor);

        Long increment = codeMapper.getCurrentIncrement(factor);
        if (increment == null) {
            increment = codeStart;
        }
        increment = increment + 1;
        code.setIncrement(increment);
        codeMapper.generateCode(code);

        StringBuilder buff = new StringBuilder();
        buff.append(factor)
                .append(increment);
        return buff.toString();
    }

    /**
     * 流水号生成
     *
     * @param factor 生成因素
     */
    @Override
    public String generateWithDigits(String factor, int digits) {
        if (StringUtils.isBlank(factor)) {
            throw new APIException(APICode.NULL_PARAMETER, "物件序列生成的因素为空！");
        }
        Code code = new Code();
        code.setFactor(factor);

        Long increment = codeMapper.getCurrentIncrement(factor);
        if (increment == null) {
            increment = codeStart;
        }
        increment = increment + 1;
        code.setIncrement(increment);
        codeMapper.generateCode(code);

        String num = String.format("%0" + digits + "d", increment);
        StringBuilder buff = new StringBuilder();
        buff.append(factor)
                .append(num);
        return buff.toString();
    }

    /**
     * 流水号生成
     *
     * @param factor 生成因素
     */
    @Override
    public String generateWithTime(String factor) {
        String timeFactor = DateUtil.getDateString(new Date(), DateUtil.YYYYMMDDHHMMSSSSS);
        if (StringUtils.isBlank(factor)) {
            throw new APIException(APICode.NULL_PARAMETER, "物件序列生成的因素为空！");
        }
        StringBuilder buff = new StringBuilder();
        buff.append(codePlatform)
                .append(factor)
                .append(timeFactor);
        return generate(buff.toString());
    }
}
